package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorHeadline;
import guoanmaker.operator.business.model.OperatorHotBusiness;
import guoanmaker.operator.business.model.OperatorHotBusinessNewForAPP;
import guoanmaker.operator.business.model.ProviderBusiness;
import guoanmaker.operator.business.modelnobind.HotBusinessVO;
import guoanmaker.operator.business.repository.OperatorHotBusinessRepository;
import guoanmaker.operator.business.repository.ProviderBusinessRepository;

@Service
public class OperatorHotBusinessService implements OperatorHotBusinessServiceInterface {
	@Autowired
	private OperatorHotBusinessRepository operatorHotBusinessRepository;
	@Autowired
	private ProviderBusinessRepository providerBusinessRepository;
	  @PersistenceContext
		private EntityManager manager;
	 @Override
	    public OperatorHotBusiness saveOperatorHotBusiness(OperatorHotBusiness operatorHotBusiness) {
	        return operatorHotBusinessRepository.save(operatorHotBusiness);
	    }
	@Override
	public List<OperatorHotBusiness> getAllOperatorHotBusinesse(String cityId) {
		// TODO Auto-generated method stub
		return operatorHotBusinessRepository.getOperatorHotBusiness(cityId);
	}
	public Map selectProvideBusinessValia(String busNumber){
		Map<String,Object> remap=new HashMap<String,Object>();
		int a=0;
		String mes="";
		List<ProviderBusiness> probus=	providerBusinessRepository.selectBusinessAndUser(busNumber);
		if(probus.size()==0){
			a=1;
			mes="该商户异常，请联系管理人员";
		}else{
			a=2;
			mes="正常";
		}
		remap.put("a", a);
		remap.put("mes", mes);
		return remap;
	}
	
	 @Transactional
	  public void delectAllBycitycode(String cityId){
		  String jpql="delete  from OperatorHotBusiness  u where u.cityId=:cityId";
		  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
			query2.setParameter("cityId", cityId);
			query2.executeUpdate();
		  
	  }
	@Override
	public HotBusinessVO selectOperatorHotBusiness(String busNumber) {
		String jpql = "select new guoanmaker.operator.business.modelnobind.HotBusinessVO(b.id,p.companyLogo,b.businessName,b.companyIntroduce,p.followNum) from "+
  				"ProvideShop p left join p.providerBusiness b inner join b.personalUser u where u.isProvider=2 and b.busNumber =:busNumber";
  		Query query = manager.createQuery(jpql);
  		query.setParameter("busNumber", busNumber);
  		List<HotBusinessVO>   hotBusinessVOs = query.getResultList();
		if (hotBusinessVOs != null && hotBusinessVOs.size() >0) {
			return hotBusinessVOs.get(0);
		}
		return null;
	}
	
}
